home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / gui / MultiImage.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  8KB  |  268 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from util import print_timing
  5. from util import to_storage, Storage
  6. import wx
  7. from gui.toolbox import TransparentBitmap
  8. from logging import getLogger
  9. log = getLogger('MultiImage')
  10. info = log.info
  11. error = log.error
  12.  
  13. class MultiImage(object):
  14.     
  15.     def __init__(self, images):
  16.         self.images = images
  17.         self.tags = { }
  18.         self.drawrects = { }
  19.         for image in self.images:
  20.             if hasattr(image, 'anchors'):
  21.                 for anchor in image.anchors:
  22.                     if 'tag' in anchor and anchor['tag'] is not None:
  23.                         self.tags[anchor['tag']] = image
  24.                         continue
  25.                 
  26.         
  27.         self.cached_result = Storage(bitmap = None, size = (0, 0))
  28.  
  29.     
  30.     def __repr__(self):
  31.         pformat = pformat
  32.         import pprint
  33.         return '<MultiImage:' + pformat(self.drawrects) + '>'
  34.  
  35.     
  36.     def __contains__(self, tagname):
  37.         return tagname in self.tags
  38.  
  39.     
  40.     def tag_rect(self, tagname):
  41.         if tagname not in self.tags:
  42.             raise ValueError('tag %s not in this MultiImage' % tagname)
  43.         
  44.         return self.drawrects[self.tags[tagname]]
  45.  
  46.     
  47.     def Draw(self, dc, rect):
  48.         position = rect[:2]
  49.         size = rect[2:]
  50.         (self.w, self.h) = size
  51.         if not (self.cached_result.bitmap) or self.cached_result.size != size:
  52.             info('new size, making bitmap')
  53.             self.recalc()
  54.             self.cached_result.size = size
  55.             self.cached_result.bitmap = self.make_bitmap(*size)
  56.         
  57.         dc.DrawBitmapPoint(self.cached_result.bitmap, position)
  58.  
  59.     
  60.     def make_bitmap(self, width, height):
  61.         size = (width, height)
  62.         bitmap = TransparentBitmap(size)
  63.         if not hasattr(self, 'temp_dc'):
  64.             self.temp_dc = wx.MemoryDC()
  65.         
  66.         self.temp_dc.SelectObject(bitmap)
  67.         self.drawtodc(self.temp_dc, wx.Rect(0, 0, *size))
  68.         self.temp_dc.SelectObject(wx.NullBitmap)
  69.         self.region = wx.RegionFromBitmapColour(bitmap, (0, 0, 0, 0))
  70.         return bitmap
  71.  
  72.     
  73.     def recalc(self):
  74.         self.drawrects.clear()
  75.         for i in self.images:
  76.             if i not in self.drawrects:
  77.                 self.compute_rect(i, wx.Rect(0, 0, self.w, self.h))
  78.                 continue
  79.         
  80.  
  81.     
  82.     def drawtodc(self, dc, rect):
  83.         for image in self.images:
  84.             drect = wx.Rect(*self.drawrects[image])
  85.             drect.Offset((rect.x, rect.y))
  86.             image.Draw(dc, drect)
  87.         
  88.  
  89.     
  90.     def compute_rect(self, image, dcrect):
  91.         myanchors = image.get_anchors_to()
  92.         numanchors = len(myanchors)
  93.         if image.style != 'static':
  94.             if not numanchors == 1 or numanchors == 2:
  95.                 raise AssertionError('%r is not static but has anchors %r' % (image, image.anchors))
  96.             
  97.             if numanchors == 1:
  98.                 anchorfrom = to_storage(myanchors[0])
  99.                 tag = anchorfrom.to
  100.                 imageto = self.tags[tag]
  101.                 anchorto = [](_[1][0])
  102.                 rectto1 = self.drawrects[imageto]
  103.                 rectto2 = (0, 0, dcrect.width, dcrect.height)
  104.                 positionto1 = compute_anchor_position(anchorto, rectto1[2:], rectto1[:2])
  105.                 positionto2 = compute_anchor_position(get_SI2_anchor(image), rectto2[2:], rectto2[:2])
  106.                 positionfrom1 = compute_anchor_position(anchorfrom, [
  107.                     image.width,
  108.                     image.height], [
  109.                     0,
  110.                     0])
  111.                 positionfrom2 = (0, 0)
  112.                 diffxto = abs(positionto1[0] - positionto2[0])
  113.                 diffyto = abs(positionto1[1] - positionto2[1])
  114.                 diffxlocal = abs(positionfrom1[0] - positionfrom2[0])
  115.                 diffylocal = abs(positionfrom1[1] - positionfrom2[1])
  116.                 increasex = diffxto - diffxlocal
  117.                 increasey = diffyto - diffylocal
  118.                 newsizew = image.width + increasex
  119.                 newsizeh = image.height + increasey
  120.                 newlocalanchorposition = positionto2
  121.                 positiontodrawat = (positionfrom2[0] + newlocalanchorposition[0], positionfrom2[1] + newlocalanchorposition[1])
  122.                 self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], newsizew, newsizeh)
  123.                 return None
  124.             elif numanchors == 2:
  125.                 (anchorfrom1, anchorfrom2) = myanchors
  126.                 tag1 = anchorfrom1['to']
  127.                 tag2 = anchorfrom2['to']
  128.                 imageto1 = self.tags[tag1]
  129.                 imageto2 = self.tags[tag2]
  130.                 anchorto1 = _[2]
  131.                 anchorto2 = _[3]
  132.                 rectto1 = self.drawrects[imageto1]
  133.                 rectto2 = self.drawrects[imageto2]
  134.                 positionto1 = compute_anchor_position(anchorto1, rectto1[2:], rectto1[:2])
  135.                 positionto2 = compute_anchor_position(anchorto2, rectto2[2:], rectto2[:2])
  136.                 positionfrom1 = compute_anchor_position(anchorfrom1, [
  137.                     image.imgw,
  138.                     image.imgh], [
  139.                     0,
  140.                     0])
  141.                 positionfrom2 = compute_anchor_position(anchorfrom2, [
  142.                     image.imgw,
  143.                     image.imgh], [
  144.                     0,
  145.                     0])
  146.                 diffxto = abs(positionto1[0] - positionto2[0])
  147.                 diffyto = abs(positionto1[1] - positionto2[1])
  148.                 diffxlocal = abs(positionfrom1[0] - positionfrom2[0])
  149.                 diffylocal = abs(positionfrom1[1] - positionfrom2[1])
  150.                 increasex = diffxto - diffxlocal
  151.                 increasey = diffyto - diffylocal
  152.                 newsizew = image.imgw + increasex
  153.                 newsizeh = image.imgh + increasey
  154.                 newlocalanchorposition = compute_anchor_position(anchorfrom1, newsizew, newsizeh, [
  155.                     0,
  156.                     0])
  157.                 positiontodrawat = (positionto1[0] - newlocalanchorposition[0], positionto1[1] - newlocalanchorposition[1])
  158.                 self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], newsizew, newsizeh)
  159.                 return None
  160.             else:
  161.                 raise AssertionError('invalid skin, wrong number (%d) of anchors for image of type %s!' % (numanchors, image.image_dictionary['style']))
  162.         elif numanchors == 0:
  163.             positiontodrawat = compute_anchor_position(get_SI2_anchor(image), (dcrect.width, dcrect.height), [
  164.                 0,
  165.                 0])
  166.             self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], image.width, image.height)
  167.             return None
  168.         elif numanchors == 1:
  169.             anchorfrom = image.get_anchors_to()[0]
  170.             tag = anchorfrom['to']
  171.             imageto = self.tags[tag]
  172.             anchorto = _[4]
  173.             rectto = self.drawrects[imageto]
  174.             positionto = compute_anchor_position(anchorto, rectto[2:], rectto[:2])
  175.             positionfrom = compute_anchor_position(anchorfrom, [
  176.                 image.width,
  177.                 image.height], [
  178.                 0,
  179.                 0])
  180.             positiontodrawat = (positionto[0] - positionfrom[0], positionto[1] - positionfrom[1])
  181.             self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], image.width, image.height)
  182.             return None
  183.         else:
  184.             raise AssertionError('invalid skin, wrong number (%d) of anchors for image of type %s!' % (numanchors, image.image_dictionary['style']))
  185.  
  186.  
  187.  
  188. def compute_anchor_position(anchor, size, offset):
  189.     if 'halign' in anchor:
  190.         halign = anchor.halign
  191.     else:
  192.         halign = 'left'
  193.     if 'valign' in anchor:
  194.         valign = anchor.valign
  195.     else:
  196.         valign = 'top'
  197.     if 'offset' in anchor:
  198.         off = anchor.offset
  199.     else:
  200.         off = [
  201.             0,
  202.             0]
  203.     if isinstance(off[0], int):
  204.         myoffsetx = off[0]
  205.     else:
  206.         myoffsetx = int(str(off[0])[:-1]) * size[0] / 100
  207.     if isinstance(off[1], int):
  208.         myoffsety = off[1]
  209.     else:
  210.         myoffsety = int(str(off[1])[:-1]) * size[1] / 100
  211.     off = [
  212.         myoffsetx,
  213.         myoffsety]
  214.     tup = (offset[0], offset[1], size[0], size[1])
  215.     if halign == 'left':
  216.         x = 0
  217.     elif halign == 'right':
  218.         x = tup[2]
  219.     else:
  220.         x = tup[2] / 2
  221.     x = x + tup[0] + off[0]
  222.     if valign == 'top':
  223.         y = 0
  224.     elif valign == 'bottom':
  225.         y = tup[3]
  226.     else:
  227.         y = tup[3] / 2
  228.     y = y + tup[1] + off[1]
  229.     return (x, y)
  230.  
  231.  
  232. def get_SI2_anchor(image):
  233.     retval = Storage()
  234.     if hasattr(image, 'offset'):
  235.         retval['offset'] = image.offset
  236.     
  237.     if hasattr(image, 'valign'):
  238.         retval['valign'] = image.valign
  239.     
  240.     if hasattr(image, 'halign'):
  241.         retval['halign'] = image.halign
  242.     
  243.     return retval
  244.  
  245.  
  246. def main(images):
  247.     temp_dc = wx.MemoryDC()
  248.     temp_dc.SelectObject(destbitmap)
  249.     mimg = MultiImage(images)
  250.     drawrect = wx.Rect(10, 10, 220, 440)
  251.     mimg.draw(temp_dc, drawrect)
  252.     temp_dc.SelectObject(wx.NullBitmap)
  253.  
  254. if __name__ == '__main__':
  255.     import util
  256.     import syck
  257.     from skins import images as imgmngr
  258.     from skins import skins
  259.     app = wx.PySimpleApp()
  260.     skins.res_path = '../../res/'
  261.     destbitmap = imgmngr.get('skins/default/blue-flower.jpg')
  262.     f = file('../../res/skins/skinExample')
  263.     images = to_storage(syck.load(f)).Images
  264.     f.close()
  265.     util.profile(main, images)
  266.     destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
  267.  
  268.